package com.dtp.logging.logback;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.util.ContextInitializer;
import com.dtp.logging.AbstractDtpLogging;
import com.dtp.logging.LogHelper;
import lombok.extern.slf4j.Slf4j;

/**
 * DtpLogbackLogging 以Logback实现的日志处理器
 *
 * @author yanhom
 * @since 1.0.5
 *
 * @remark muse
 */
@Slf4j
public class DtpLogbackLogging extends AbstractDtpLogging {
    private static final String LOGBACK_LOCATION = "classpath:dtp-logback.xml"; // 日志配置文件所在路径
    private LoggerContext loggerContext;                                        // logback包中的日志上下文

    /**
     * ContextInitializer是被用来初始化LoggerContext
     * 通过调用configureByResource方法，可以依据在类路径下的logback配置文件（dtp-logback.xml）来配置LoggerContext
     */
    @Override
    public void loadConfiguration() {
        try {
            loggerContext = new LoggerContext();
            new ContextInitializer(loggerContext).configureByResource(getResourceUrl(LOGBACK_LOCATION));
        } catch (Exception e) {
            log.error("Cannot initialize dtp logback logging.");
        }
    }

    public LoggerContext getLoggerContext() {return loggerContext;}

    /** 初始化LogHelper中的monitorLogger变量 */
    @Override
    public void initMonitorLogger() {
        LogHelper.init(getLoggerContext().getLogger(MONITOR_LOG_NAME));
    }
}
